﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Ext.Net;
using System.Collections.Generic;

public partial class Popup_Popup_Mat3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DateTime dt = DateTime.Now;
            this.dtFrom.Text = dt.Year.ToString() + "-" + "01" + "-" + "01";
            this.dtTo.Text = dt.Year.ToString() + "-" + dt.Month.ToString() + "-" + dt.Day.ToString();

            this.SetListViewData(null, null);
        }
    }

    #region == ListView1_SelectionChange(업체선택) ==
    protected void ListView1_SelectionChange(object sender, DirectEventArgs e)
    {
        string[] strParams = this.GetJsonToString(e.ExtraParams["Values"]);

        if (strParams.Length > 1)
        {
            this.hdREGNO.Text = strParams[1];
            using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
            {
                string strFrom = this.dtFrom.Text.Substring(0, 10).Replace("-", "");
                string strTo = this.dtTo.Text.Substring(0, 10).Replace("-", "");

                DataSet ds = oService.Select_D_DAYWRITE_POP2_D_ITEM_S(Session["PROJ_CD"].ToString(), Session["DT"].ToString(), this.hdREGNO.Text, this.tbxMatName.Text, strFrom, strTo);

                this.Store2.RemoveAll();

                this.Store2.DataSource = ds.Tables[0];
                this.Store2.DataBind();
            }
        }
    }
    #endregion

    #region == 업체명 리스트뷰 바인드 ==
    protected void SetListViewData(object sender, StoreRefreshDataEventArgs e)
    {
        try
        {
            //프로젝트 코드 가져오기
            string strProjCd = string.Empty;
            if (Session["PROJ_CD"] != null)
            {
                strProjCd = Session["PROJ_CD"].ToString();
            }

            using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
            {
                DataSet ds = oService.Select_D_DAYWRITE_POP2_D_CUST_S(this.tbxCustomer.Text);
                if (ds != null)
                {
                    List<object> data = new List<object>(ds.Tables[0].Rows.Count);
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        data.Add(new
                        {
                            BIZ_REGNO = dr["BIZ_REGNO"].ToString(),
                            COMP_NM = dr["COMP_NM"].ToString(),
                            REPR_NM = dr["REPR_NM"].ToString(),
                        });
                    }

                    this.ListView1.SelectedRow = null;
                    var store = this.ListView1.GetStore();
                    store.DataSource = data;
                    store.DataBind();

                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        this.ListView1.Select(new int[] { 0 });
                    }

                }
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    #endregion


    protected void CreateParameter(object sender, DirectEventArgs e)
    {
        string json = e.ExtraParams["Values"];

        Dictionary<string, string>[] dicValues = JSON.Deserialize<Dictionary<string, string>[]>(json);
        string strVal = string.Empty;

        foreach (Dictionary<string, string> dic in dicValues)
        {
            foreach (KeyValuePair<string, string> row in dic)
            {
                strVal += row.Value + "|";
            }
        }

        strVal = strVal.Remove(strVal.Length - 1);
        hdRowValue.Text = strVal;
    }


    #region == Jason To String ==
    /// <summary>
    /// Json을 string[]으로...
    /// </summary>
    /// <param name="pJson"></param>
    /// <returns></returns>
    private string[] GetJsonToString(string pJson)
    {
        return pJson.Replace("[{", "").Replace(":", "").Replace(",", "").Replace("}]", "").Split(new string[] { "\"" }, StringSplitOptions.RemoveEmptyEntries);
    }
    #endregion


    #region 조회
    protected void PopListSearch(object sender, DirectEventArgs e)
    {
        SetGrid();
    }


    protected void SetGrid()
    {
        using (EmdisBest.WebService.Service oService = new EmdisBest.WebService.Service())
        {
            DataSet ds = null;

            if (!string.IsNullOrEmpty(this.tbxMatName.Text))
            {
                string strFrom = this.dtFrom.Text.Substring(0, 10).Replace("-", "");
                string strTo = this.dtTo.Text.Substring(0, 10).Replace("-", "");

                this.Store1.RemoveAll();
                this.Store2.RemoveAll();

                ds = oService.Select_D_DAYWRITE_POP2_D_CUST_S(this.tbxCustomer.Text);
                this.Store1.DataSource = ds.Tables[0];
                this.Store1.DataBind();

                ds = oService.Select_D_DAYWRITE_POP2_D_ITEM_S(Session["PROJ_CD"].ToString(), Session["DT"].ToString(), this.hdREGNO.Text, this.tbxMatName.Text, strFrom, strTo);
                this.Store2.DataSource = ds.Tables[0];
                this.Store2.DataBind();
            }
            else
            {
                ds = oService.Select_D_DAYWRITE_POP2_D_CUST_S(this.tbxCustomer.Text);
                this.Store1.DataSource = ds.Tables[0];
                this.Store1.DataBind();

                this.Store2.RemoveAll();
                this.hdREGNO.Text = "";
            }

            this.Store1.DataSource = ds;
            this.Store1.DataBind();
        }
    }
    #endregion


    protected void btnInser_Click(object sender, DirectEventArgs e)
    {
        var win = new Window
        {
            ID = "Window2",
            //Title = "My Window",
            Height = 350,
            Width = 700,
            Padding = 5
        };


        Ext.Net.Button btn = (Ext.Net.Button)sender;
        string id = btn.ID;

        switch (id)
        {
            case "btnMatI":
                win.Listeners.Hide.Handler = "btnMatI.setDisabled(false);";
                win.AutoLoad.Url = "Popup_Mat_Add.aspx?regno=" + this.hdREGNO.Text;
                win.Title = "자재 현황 업체별발주단가 추가";
                win.AutoLoad.Mode = LoadMode.IFrame;
            ;
                win.Render(this.Form);
                break;

            case "btnMatU":
                win.Listeners.Hide.Handler = "btnMatU.setDisabled(false);";
                win.AutoLoad.Url = "Popup_People_Add.aspx?code=" + this.hdRowValue.Text.Split('|')[0];
                win.Title = "인원 현황 추가항목";
                win.AutoLoad.Mode = LoadMode.IFrame;
                win.Render(this.Form);
                break;
            default:
                break;
        }
    }
}
